package edu.whu.puppygo.sacnner;

import edu.whu.puppygo.crawler.PetNews;
import edu.whu.puppygo.crawler.PetNewsController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.pipeline.CollectorPageModelPipeline;

import java.util.List;

/**
 * @author guanruwang
 * @date 2023/12/11$
 */
@Component
@Slf4j
public class petNewsListener {

    private static final String URL = "https://www.chongwunet.com/cjxw/";

    /**
     * &#064;description:   每10分钟爬取一次宠物新闻
     */
    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void scan() {
        CollectorPageModelPipeline<PetNews> collectorPageModelPipeline = new CollectorPageModelPipeline<PetNews>();
        OOSpider.create(
                Site.me(),
                collectorPageModelPipeline, PetNews.class).addUrl(URL).run();
        List<PetNews> news = collectorPageModelPipeline.getCollected();
        log.info("获取到新闻数量: {}", news.size());
        PetNewsController.petNews = news;
    }
}
